package com.yuyou.fn.updater.handler;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.wens.mybatisplus.toolkit.IdWorker;
import com.google.common.base.Optional;
import com.google.common.cache.LoadingCache;
import com.yuyou.fn.activity.entity.Activity;
import com.yuyou.fn.activity.entity.ActivityRegSituation;
import com.yuyou.fn.activity.service.IActivityRegSituationService;
import com.yuyou.fn.activity.service.IActivityService;
import com.yuyou.fn.common.database.event.EventHandler;
import com.yuyou.fn.common.database.event.IEventHandler;
import com.yuyou.fn.common.util.*;
import com.yuyou.fn.crm.entity.CrmStudentCommunicate;
import com.yuyou.fn.crm.service.ICrmStudentCommunicateService;
import com.yuyou.fn.crm.vo.CrmStudentCommunicateQueryVo;
import com.yuyou.fn.educational.entity.*;
import com.yuyou.fn.educational.service.*;
import com.yuyou.fn.platform.entity.BusTeam;
import com.yuyou.fn.platform.service.IBusTeamService;
import com.yuyou.fn.report.entity.MergeStudentRegDetail;
import com.yuyou.fn.report.entity.StudentRegDetail;
import com.yuyou.fn.report.service.IMergeStudentRegDetailService;
import com.yuyou.fn.report.service.IStudentRegDetailService;
import com.yuyou.fn.settlement.service.IPayRecordService;
import org.apache.commons.lang3.StringUtils;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.concurrent.ExecutionException;
/**
*@Description: 根据报读更新参加活动的学生的报读状态
*@Param: 
*@return: 
*@Author: lzh
*@date: 
*/
@EventHandler(tables = {"yyfn.student_reg"})
@Component
public class StudentActivityReadUpdater implements IEventHandler {

  //  private final static Logger log = LoggerFactory.getLogger(StudentRegStateUpdater.class);

    @Resource
    private IStudentRegService studentRegService;

    @Resource
    private IStudentService studentService;

    @Resource
    private IActivityRegSituationService activityRegSituationService;

    @Resource
    private IActivityService activityService;

    @Resource
    private IStudentRegDetailService studentRegDetailService;

    @Resource
    private IPeriodService periodService;

    @Resource
    private IExternalRegDetailService externalRegDetailService;


    private final Logger log = LoggerFactory.getLogger(StudentActivityReadUpdater.class);

    private LoadingCache<Long,Optional<Student>> studentLoadingCache ;

    @PostConstruct
    public void init(){

        this.studentLoadingCache = CacheUtils.buildCache(studentService::findById) ;
    }




    @Override
    public void insert(String tableName, JSONArray rowList) {
        build(tableName, rowList);
    }

    @Override
    public void update(String tableName, JSONArray rowList, Set<String> updateColumns) {
        build(tableName, rowList);
    }

    @Override
    public void delete(String tableName, JSONArray rowList) {

    }

    private void build(String tableName, JSONArray rowList) {
        for (int i = 0; i < rowList.size(); i++) {
            JSONObject row = rowList.getJSONObject(i);
            final StudentReg studentReg = row.toJavaObject(StudentReg.class);
            if (studentReg.getChargingStatus()==2){
                try {
                    Student student=studentLoadingCache.get(studentReg.getStudentId()).get();
                    List<Activity> activityList=activityService.findActivityListByPeriodId(studentReg.getPeriodId());
                    List<Long> activityIds = BeanUtils.toList(activityList, "activityId");
                    List<String> studentPhoneNos=new ArrayList<>();
                    studentPhoneNos.add(student.getPhoneNo());
                    if (StringUtils.isNotEmpty(student.getSlavePhoneNo())){
                        studentPhoneNos.add(student.getSlavePhoneNo());
                    }

                    List<ActivityRegSituation> activityRegSituationList = activityRegSituationService.findByActivityIdsStudentNamePhoneNosReadStatus(activityIds, student.getName(), studentPhoneNos, 0);
                    List<Long> activityRegSituationIds = BeanUtils.toList(activityRegSituationList, "activityRegSituationId");
                    activityRegSituationService.updateReadStatusFirstReadTimeByIds(activityRegSituationIds,1,studentReg.getCreateTime());
                }catch (Exception e){
                    log.error("处理参与活动的学生是否报读失败，报读id为:{}的",studentReg.getRegId());
                }

            }
        }
    }



}
